package com.lmk.service.impl;

import com.lmk.domain.Product;
import com.lmk.domain.Store;
import com.lmk.feign.StoreServices;
import com.lmk.mapper.ProductMapper;
import com.lmk.service.ProductService;
import com.lmk.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreServices storeService;
   /* @Autowired
    private StoreMapper storeMapper;*/
    @Override
    public List<Product> getAll() {
        return productMapper.getAll();
    }

    @Override
    public Product getById(Long id) {
        return productMapper.getById(id);
    }

    @Override
    @Transactional
    public AjaxResult add(Product product) {
        productMapper.add(product);
        Store store = new Store();
        store.setProduct_id(product.getId());
        store.setNumber(product.getNumber());
        AjaxResult result = storeService.addorupdate(store);
        //如果返回状态为false就删掉
        if (result.getSuccess() == false){
            productMapper.del(product.getId());
        }
        return result;
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public void del(Long id) {
        productMapper.del(id);
    }
}
